/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.seagate.app.manager;

import com.seagate.app.bean.UserBean;
import com.seagate.util.db.OracleConnectionPool;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 452530
 */
public class UserRole {
public List getUserList() {
        List userlist = new ArrayList();
        String sql = "SELECT GID,ROLE FROM AMA_STDPART_USER ";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        try {
            conn = OracleConnectionPool.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery(sql);
            while (rset.next() == true) {
                UserBean ub=new UserBean();
                ub.setGid(rset.getString(1));
                ub.setRole(rset.getString(2));

                userlist.add(ub);


            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rset.close();
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
            }
        }

        return userlist;
    }

public UserBean getUser(String gid) {
        UserBean user = new UserBean();
        String sql = "SELECT GID,ROLE FROM AMA_STDPART_USER WHERE GID='"+gid+"'";
        Connection conn = null;
        Statement stmt = null;
        ResultSet rset = null;
        try {
            conn = OracleConnectionPool.getConnection();
            stmt = conn.createStatement();
            rset = stmt.executeQuery(sql);
            while (rset.next() == true) {

                user.setGid(rset.getString(1));
                user.setRole(rset.getString(2));

               return user;


            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                rset.close();
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
            }
        }

        return null;
    }

    public boolean addUser(String gid, String role) {
        boolean result = false;
        String sql = "";
          sql = "insert into AMA_STDPART_USER (GID,ROLE) "
                    + " values ('" + gid + "','" + role + "')";

        Connection conn = null;
        Statement stmt = null;

        try {
            conn = OracleConnectionPool.getConnection();
            stmt = conn.createStatement();

            stmt.executeUpdate(sql);
            result = true;

        } catch (SQLException e) {
            System.err.println("sql = " + sql);
            System.err.println("code = " + e.getLocalizedMessage());
            result = false;
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
            }
        }

        return result;
    }
    public boolean editUser(String gid, String role) {
        boolean result = false;
        String sql = "";
          sql = "update AMA_STDPART_USER set ROLE = '" + role + "'  where GID = '" + gid + "'";


        Connection conn = null;
        Statement stmt = null;

        try {
            conn = OracleConnectionPool.getConnection();
            stmt = conn.createStatement();

            stmt.executeUpdate(sql);
            result = true;

        } catch (SQLException e) {
            System.err.println("sql = " + sql);
            System.err.println("code = " + e.getLocalizedMessage());
            result = false;
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
            }
        }

        return result;
    }
    public boolean deleteUser(String gid) {
        boolean result = false;
        String sql = "";
          sql = "delete AMA_STDPART_USER  where GID = '" + gid + "'";


        Connection conn = null;
        Statement stmt = null;

        try {
            conn = OracleConnectionPool.getConnection();
            stmt = conn.createStatement();

            stmt.executeUpdate(sql);
            result = true;

        } catch (SQLException e) {
            System.err.println("sql = " + sql);
            System.err.println("code = " + e.getLocalizedMessage());
            result = false;
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (SQLException ex) {
            }
        }

        return result;
    }
}
